import {getsmsCode} from "@/api/login";

let time = 600000;// 滑块超时时间  10分钟一直显示滑块
let errorNumber = 3;// 错误3次 触发
export const getCode = {
    data() {
        return {
            btnShow: true, //验证码是否允许点击
            voiceShow: false, //语音验证码是否显示
            counter: "获取短信验证码",
            message: "验证码已发送至您的手机，有效期10分钟，请尽快填写",
            verifyShow: false,//触发滑块显示
            doAction: "",//短信或者密码登录触发标识
        }
    },
    methods: {
        getCode(mobile, smsType, message) {
            getsmsCode(mobile, smsType).then(
                (res) => {
                    if (res.code === 200) {
                        this.btnShow = false;
                        uni.showToast({
                            title: message || this.message,
                            // type: "success",
                            duration: 3000
                        });
                        this.counter = this.$smsCounter;
                        const fn = setInterval(() => {
                            this.counter--
                            if (this.counter < 0) {
                                clearInterval(fn)
                                this.counter = '重新获取'
                                this.btnShow = true;
                                this.voiceShow = true;
                            }
                        }, 1000);
                    } else {
                        this.$modal.msgError("验证码发送失败，请尝试重新登录")
                    }
                }
            );
        },
        // 关闭滑块弹出
        onClose() {
            this.verifyShow = false;
        },
        // 滑块验证成功
        touchSliderResult(result) {
            this.verifyShow = false;
            uni.setStorageSync("verifyNumber", errorNumber - 1, time);
            // 获取执行回调
            if(!!result.doAction){
               eval(result.doAction)
            }
        },
        // 检查方法 引入 提交检测
        sliderVerify() {
            let verifyNumber = uni.getStorageSync("verifyNumber");
            if (verifyNumber >= errorNumber) {
                this.verifyShow = true;
                return true;
            }
        },
        // 错误次数记录 在catch中记录
        addVerifyNumber() {
            // 触发密码错误3次 显示校验滑块
            let verifyNumber = uni.getStorageSync("verifyNumber");
            if (!verifyNumber) {
                uni.setStorageSync("verifyNumber", 1, time);
            } else {
                uni.setStorageSync("verifyNumber", verifyNumber + 1, time);
            }
        },
        // 检查方法 并且记录错误次数
        sliderVerifyAddVerifyNumber() {
            if (this.sliderVerify()) {
                return true;
            }
            this.addVerifyNumber();
        }
    }
}
